package first.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * load-on-startup
 * 1元素标记容器是否应该在web应用程序启动的时候就加载这个servlet。
 * 2它的值必须是一个整数，表示servlet被加载的先后顺序。
 * 3如果该元素的值为负数或者没有设置，则容器会当Servlet被请求时再加载。
 * 4如果值为正整数或者0时，表示容器在应用启动时就加载并初始化这个servlet，值越小，servlet的优先级越高，就越先被加载。
 *  值相同时，容器就会自己选择顺序来加载。
 *  <init-param>
 *       <param-name>name</param-name>
 *       <param-value>张三</param-value>
 *  </init-param>
 * 1 init-param元素用来定义Servlet启动的参数，可以定义多个
 * 2 param-name表示参数名称
 * 3 param-value表示参数值
 */
@WebServlet(name = "MyServlet3", value = "/servlet3", loadOnStartup = 0,
        initParams = {@WebInitParam(name = "username",value = "张三"),@WebInitParam(name = "age",value = "20")})
public class MyServlet3 extends HttpServlet {

    public void init() throws ServletException {
        super.init();
        System.out.println("MyServlet3初始化了.............");
        ServletConfig servletConfig = getServletConfig();
        String username = servletConfig.getInitParameter("username");
        String age = servletConfig.getInitParameter("age");
        System.out.println(username+" "+age);

    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("MyServlet3启动了");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
